package com.yuxiu.edu.web.controller;

import com.yuxiu.edu.model.User;
import com.yuxiu.edu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public String login(String username, String password, HttpSession session){
        System.out.println("==================================================");
        //数据库查询username对应password
        User user  = userService.findByPassword(username);
        //get方法获取username对应password
        //System.out.println(user.getPassword());
        System.out.println("==================================================");
        //比较来源与结果
        if((user.getPassword()).equals(password)){
            //登录成功
            //1.保存session
            session.setAttribute("user",username);
            //2.进入主页面
            return "Default";
        }
        return "login";
    }

    @RequestMapping("select")
    public String select(String text,Model model){
        System.out.println("==================================================");
        //数据库查询username对应password
        if(text !=null) {
            List<User> userList = userService.findByText(text);
            System.out.println(userList);
            System.out.println("==================================================");
            if (userList != null) {
                model.addAttribute("userList", userList);
                return "User/UserInfo";
            }
            else{
                List<User> user= userService.findByAll();
                System.out.println(user);
                //2.存数据
                model.addAttribute("userList",user);
                return "User/UserInfo";
            }
        }
        else{
            List<User> userList= userService.findByAll();
            System.out.println(userList);
            //2.存数据
            model.addAttribute("userList",userList);
            return "User/UserInfo";
        }
    }

    @RequestMapping("list")
    public String list(Model model){
        //1.查数据
        List<User> userList= userService.findByAll();
        System.out.println(userList);
        //2.存数据
        model.addAttribute("userList",userList);
        return "User/UserInfo";
    }

    @RequestMapping("find")
    public String find(Integer id,Model model){
        //1.查数据
        System.out.println("id:" + id);
        User user  = userService.findById(id);
        System.out.println(user);
        //2.存数据
        if (user != null){
            model.addAttribute("user",user);
        }
        return "User/UserInfo";
    }

    @RequestMapping("delete")
    public String delete(Integer id){
        //保存数据
        System.out.println("=======================删除===========================");
        userService.deleteById(id);
        System.out.println("========================删除==========================");
        return "forward:list.do";
    }

    @RequestMapping("edit")
    public String edit(){
        System.out.println("======================新增页面===========================");
        return "User/UserEdit";
    }

    @RequestMapping("update")
    public String update(Integer id,Model model){
        System.out.println("======================修改页面===========================");
        System.out.println("id:" + id);
        System.out.println("=======================修改===========================");
        //通过id找到商品
        User user = userService.findById(id);
        System.out.println(user);
        if (user != null){
            model.addAttribute("user",user);
        }
        return "User/UserUpdate";
    }

    @RequestMapping("save")
    public String save(String username, String password,Integer id){
        //创建商品
        User userL  = userService.findById(id);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
//        user.setId(userL.getId());
        System.out.println("=======================有无ID===========================");
//        System.out.println(user.getId());
//        items.setCreatetime(new Date());

        //保存数据
        userService.saveOrUpdate(user);

        return "forward:list.do";
    }

    @RequestMapping("manage")
    public String manage(){
        return "User/UserManage";
    }

    @RequestMapping("info")
    public String info(){
        return "User/UserInfo";
    }

    @RequestMapping("other")
    public String other(){
        return "User/UserOther";
    }
}
